//
//  SYChooseImageViewController.m
//  SanYiTeaching
//
//  Created by 陈豪 on 2020/5/15.
//  Copyright © 2020 SanYi. All rights reserved.
//

#import "SYChooseImageViewController.h"
#import "SYChooseImageViewCell.h"

@interface SYChooseImageViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (nonatomic ,strong) NSArray *DataArr;
@property (nonatomic ,assign) NSInteger nowIndex;// 现在是第几层
@property (nonatomic ,strong) NSString *chooseId;// 选择科目Id


@property (nonatomic ,strong) NSString *isImage;// 是文件还是图片


@end

@implementation SYChooseImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view from its nib.
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [self.collectionView setCollectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"SYChooseImageViewCell" bundle:nil] forCellWithReuseIdentifier:@"SYChooseImageViewCell"];
    self.isImage = @"0";
    self.nowIndex = 0;
    
    [self getKeMuInfoRequest];
}
-(void)getKeMuInfoRequest{
    
    [SYRequestAPI postDataFromURLScheme:@"resource/MyResourceService.svc/GetSubjectInfo" parameterDictionary:nil success:^(id responseObject) {
        
        self.DataArr = responseObject[@"data"];
        
        [self.collectionView reloadData];
         
    } fail:^(NSError *error) {
        
    }];
}
#pragma mark - coll
/**
 分区个数
 */
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

/**
 每个分区item的个数
 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.DataArr.count;
}
/**
 创建cell
 */
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *dic = self.DataArr[indexPath.row];
    SYChooseImageViewCell *homeCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SYChooseImageViewCell" forIndexPath:indexPath];
    __weak typeof(self) weakSelf = self;
    if (self.nowIndex == 0) {
        homeCell.goodsImage.image = [UIImage imageNamed:@"wenJian_file"];
        homeCell.goodsName.text = [dic nilObjectForKey:@"name"];
        
    }else{
        if ([self.isImage isEqualToString:@"0"]) {
            
            homeCell.goodsImage.image = [UIImage imageNamed:@"wenJian_file"];
            homeCell.goodsName.text = [dic nilObjectForKey:@"name"];
            
        }else{
            
            NSString *imageUrl =  [[NSString stringWithFormat:@"%@",[dic nilObjectForKey:@"imagePath"]] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

            [homeCell.goodsImage sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:nil];
            
            homeCell.goodsName.text = [dic nilObjectForKey:@"templateTypeName"];
        }

    }

    return homeCell;
}



/**
 cell的大小
 */
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(125, 174);
}
/**
 每个分区的内边距（上左下右）
 */
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(50, 40, 50, 40);
}
/**
 分区内cell之间的最小行间距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 50;
}
/**
 分区内cell之间的最小列间距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return 40;
}
/**
 点击了某个item
 */
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *dic = self.DataArr[indexPath.row];
    
    if (self.nowIndex == 0) {
        
        self.chooseId = [NSString stringWithFormat:@"%@",[dic nilObjectForKey:@"id"]];
        [self GetKeShiInfoRequestWithChooseId:@""];
        
    }else{
        if ([self.isImage isEqualToString:@"0"]) {
            
            if ([[dic nilObjectForKey:@"childCount"]integerValue] == 0) {
                
                [self GetKeShiImageInfoRequestWithChooseId:[NSString stringWithFormat:@"%@",[dic nilObjectForKey:@"lessonId"]]];
                
            }else{
                
                [self GetKeShiInfoRequestWithChooseId:[NSString stringWithFormat:@"%@",[dic nilObjectForKey:@"lessonId"]]];
            }

        }else{
            
            NSString *imageUrl =  [[NSString stringWithFormat:@"%@",[dic nilObjectForKey:@"imagePath"]] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

            if (self.chooseImage) {
                self.chooseImage(imageUrl);
                
            }
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
//            homeCell.goodsName.text = [dic nilObjectForKey:@"templateTypeName"];
            
        }
    }
    
}
//获取课时信息
-(void)GetKeShiInfoRequestWithChooseId:(NSString *)choId{
    NSDictionary *parame ;
    
    if (self.nowIndex == 0) {
        parame = @{
            @"subjectId":self.chooseId,
            @"parentId":choId
        };
    }else{
        parame = @{
            @"subjectId":self.chooseId,
            @"parentId":choId
        };
    }

    [SYRequestAPI postDataFromURLScheme:@"resource/MyResourceService.svc/GetLessonInfoBySubjectId" parameterDictionary:parame success:^(id responseObject) {
    
    self.nowIndex = self.nowIndex + 1;

    self.DataArr = responseObject[@"data"];
        
    [self.collectionView reloadData];
         
    } fail:^(NSError *error) {
        
    }];
}
//获取课时下面图片信息
-(void)GetKeShiImageInfoRequestWithChooseId:(NSString *)choId{
    self.isImage = @"1";
    NSDictionary *parame = @{
        @"lessonId":choId
    };

    [SYRequestAPI postDataFromURLScheme:@"resource/MyResourceService.svc/GetTemplateFileInfo" parameterDictionary:parame success:^(id responseObject) {
    

    self.DataArr = responseObject[@"data"];
        
    [self.collectionView reloadData];
         
    } fail:^(NSError *error) {
        
    }];
}
- (IBAction)tuiChuBtnClick:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


@end
